<?php
/*
 * @Description    : 微信公众平台入口
 * @Author         : QianLong
 * @Date           : 2022-07-18 10:55:08
 * @LastEditors    : QianLong
 * @LastEditTime   : 2024-05-15 10:42:59
 */

declare(strict_types=1);

namespace App\Controller;

use App\Constants\Status;
use App\Exception\ApiException;
use App\Job\SignFailAddJob;
use Qianlong\Validate\Validate;
class MpWeixinApiController extends AbstractController
{
  public $mpInfo = [];
  public function init($param)
  {
    if (empty($param['mw_id'])) {
      throw new ApiException(-1, 'mw_id 不能为空');
    }
    $this->mpInfo = $this->redisCache->getMpWxInfo($param['mw_id']);
  }
  public function getAllMedia()
  {
    $validate = new Validate();
    $validate->addColumn('mw_id')->required()->notEmpty();
    if ($validate && !$this->validate($validate)) {
      throw new ApiException(Status::CODE_BAD_REQUEST, $validate->getError()->__toString());
    }
    unset($validate);
    $param = $this->request->all();
    $offset = ($param['page']-1)*20;
    $allMedia = $this->mpWeixin->getAllMedia($param['mw_id'], $param['type'], $offset);
    return $this->response->json(200, 'success', $allMedia);
  }
  public function createMenu()
  {
    $validate = new Validate();
    $validate->addColumn('mw_id')->required()->notEmpty();
    if ($validate && !$this->validate($validate)) {
      throw new ApiException(Status::CODE_BAD_REQUEST, $validate->getError()->__toString());
    }
    unset($validate);
    $param = $this->request->all();
    $result = $this->mpWeixin->createMenu($param['mw_id'], $param['content']);
    if ($result['errcode'] == 0) {
      return $this->response->json(200, '推送成功', []);
    }
    return $this->response->json(-1, 'fail', $result['errmsg']);
  }
  public function sendTemplateMsg()
  {
    $validate = new Validate();
    $validate->addColumn('mw_id')->required()->notEmpty();
    $validate->addColumn('touser')->required()->notEmpty();
    $validate->addColumn('template_id')->required()->notEmpty();
    $validate->addColumn('data')->required();
    if ($validate && !$this->validate($validate)) {
      throw new ApiException(Status::CODE_BAD_REQUEST, $validate->getError()->__toString());
    }
    unset($validate);
    $param = $this->request->all();
    $this->init($param);
    $commonService = new \App\Service\Common;
    $newSign2 = $commonService->generateSignature($param, $this->mpInfo['server_secret']);
    if ($param['sign'] != $newSign2['sign']) {
      $logData['url'] = $this->request->fullUrl();
      $logData['remoteIp'] = $this->dataResource->getClientIp($this->request);
      $logData['user_sign'] = $param['sign'];
      $logData['uid'] = $this->mpInfo['uid'];
      $logData['server_sign'] = $newSign2['sign'];
      $logData['server_query'] = $newSign2['query'];
      queue_push(new SignFailAddJob($logData));
      return $this->response->json(Status::CODE_FAIL, 'sign签名校验失败');
    }
    $param['url'] = (!isset($param['url']))?'': $param['url'];
    $param['miniprogram'] = (!isset($param['miniprogram']))?'': $param['miniprogram'];
    $result = $this->mpWeixin->sendTemplateMsg($param['mw_id'], $param['touser'], $param['template_id'], $param['data'], $param['url'], $param['miniprogram']);
    if ($result['errcode'] == 0) {
      return $this->response->json(200, '发送成功', []);
    }
    return $this->response->json(-1, 'fail', $result['errmsg']);
  }
  public function sendKfMsg()
  {
    $validate = new Validate();
    $validate->addColumn('mw_id')->required()->notEmpty();
    $validate->addColumn('touser')->required()->notEmpty();
    $validate->addColumn('msg_type')->required()->notEmpty();
    $validate->addColumn('data')->required();
    if ($validate && !$this->validate($validate)) {
      throw new ApiException(Status::CODE_BAD_REQUEST, $validate->getError()->__toString());
    }
    unset($validate);
    $param = $this->request->all();
    $this->init($param);
    $commonService = new \App\Service\Common;
    $newSign2 = $commonService->generateSignature($param, $this->mpInfo['server_secret']);
    if ($param['sign'] != $newSign2['sign']) {
      $logData['url'] = $this->request->fullUrl();
      $logData['remoteIp'] = $this->dataResource->getClientIp($this->request);
      $logData['user_sign'] = $param['sign'];
      $logData['uid'] = $this->mpInfo['uid'];
      $logData['server_sign'] = $newSign2['sign'];
      $logData['server_query'] = $newSign2['query'];
      queue_push(new SignFailAddJob($logData));
      return $this->response->json(Status::CODE_FAIL, 'sign签名校验失败');
    }
    $result = $this->mpWeixin->sendKfMsg($param['mw_id'], $param['touser'], $param['msg_type'], $param['data']);
    if ($result['errcode'] == 0) {
      return $this->response->json(200, '发送成功', []);
    }
    return $this->response->json(-1, 'fail', $result['errmsg']);
  }
  public function getQrcode()
  {
    $validate = new Validate();
    $validate->addColumn('mw_id')->required()->notEmpty();
    $validate->addColumn('action_name')->required()->notEmpty();
    if ($validate && !$this->validate($validate)) {
      throw new ApiException(Status::CODE_BAD_REQUEST, $validate->getError()->__toString());
    }
    unset($validate);
    $param = $this->request->all();
    $this->init($param);
    $commonService = new \App\Service\Common;
    $newSign2 = $commonService->generateSignature($param, $this->mpInfo['server_secret']);
    if ($param['sign'] != $newSign2['sign']) {
      $logData['url'] = $this->request->fullUrl();
      $logData['remoteIp'] = $this->dataResource->getClientIp($this->request);
      $logData['user_sign'] = $param['sign'];
      $logData['uid'] = $this->mpInfo['uid'];
      $logData['server_sign'] = $newSign2['sign'];
      $logData['server_query'] = $newSign2['query'];
      queue_push(new SignFailAddJob($logData));
      return $this->response->json(Status::CODE_FAIL, 'sign签名校验失败');
    }
    $result = $this->mpWeixin->getQrcode($param['mw_id'], $param);
    if ($result['errcode'] == 0) {
      $result['qrcode_pic'] = 'https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket='. $result['ticket'];
      return $this->response->json(200, 'success', $result);
    }
    return $this->response->json(-1, 'fail', $result['errmsg']);
  }
  public function getAccessToken()
  {
    $validate = new Validate();
    $validate->addColumn('mw_id')->required()->notEmpty();
    if ($validate && !$this->validate($validate)) {
      throw new ApiException(Status::CODE_BAD_REQUEST, $validate->getError()->__toString());
    }
    unset($validate);
    $param = $this->request->all();
    $this->init($param);
    $commonService = new \App\Service\Common;
    $newSign2 = $commonService->generateSignature($param, $this->mpInfo['server_secret']);
    if ($param['sign'] != $newSign2['sign']) {
      $logData['url'] = $this->request->fullUrl();
      $logData['remoteIp'] = $this->dataResource->getClientIp($this->request);
      $logData['user_sign'] = $param['sign'];
      $logData['uid'] = $this->mpInfo['uid'];
      $logData['server_sign'] = $newSign2['sign'];
      $logData['server_query'] = $newSign2['query'];
      queue_push(new SignFailAddJob($logData));
      return $this->response->json(Status::CODE_FAIL, 'sign签名校验失败');
    }
    $accessToken = $this->redisCache->getWechatAccessToken($this->mpInfo['app_id'], $this->mpInfo['app_secret']);
    return $this->response->json(200, 'success', $accessToken);
  }
}
